package com.yunyouyi.cpicture.interfaces.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunyouyi.cpicture.infrastructure.annotation.AuthCheck;
import com.yunyouyi.cpicture.infrastructure.common.DeleteRequest;
import com.yunyouyi.cpicture.infrastructure.common.Result;
import com.yunyouyi.cpicture.interfaces.assembler.UserAssembler;
import com.yunyouyi.cpicture.interfaces.dto.user.*;
import com.yunyouyi.cpicture.domain.user.constant.UserConstant;
import com.yunyouyi.cpicture.infrastructure.exception.ErrorCode;
import com.yunyouyi.cpicture.infrastructure.exception.ThrowUtils;
import com.yunyouyi.cpicture.domain.user.entity.User;
import com.yunyouyi.cpicture.interfaces.vo.user.UserLoginVO;
import com.yunyouyi.cpicture.interfaces.vo.user.UserVO;
import com.yunyouyi.cpicture.application.service.UserApplicationService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserApplicationService userApplicationService;

    /**
     * 用户注册
     * @param userRegisterDTO
     * @return 用户id
     */
    @PostMapping("/register")
    public Result<Long> userRegister(@RequestBody UserRegisterDTO userRegisterDTO) {
        long result = userApplicationService.userRegister(userRegisterDTO);
        return Result.success(result);
    }

    /**
     * 用户登录
     * @param userLoginDTO
     * @param request
     * @return 脱敏后的登录用户信息
     */
    @PostMapping("/login")
    public Result<UserLoginVO> userLogin(@RequestBody UserLoginDTO userLoginDTO, HttpServletRequest request) {
        UserLoginVO userLoginVO = userApplicationService.userLogin(userLoginDTO, request);
        return Result.success(userLoginVO);
    }

    /**
     * 获取用户信息
     * @param request
     * @return 脱敏后的登录用户信息
     */
    @GetMapping("/get/login")
    public Result<UserLoginVO> getLoginUser(HttpServletRequest request) {
        User userLogin = userApplicationService.getLoginUser(request);
        return Result.success(userApplicationService.getLoginUserVO(userLogin));
    }
    /**
     * 用户退出
     * @param request
     * @return 无
     */
    @PostMapping("/logout")
    public Result<Boolean> userLogout(HttpServletRequest request) {
        return  Result.success(userApplicationService.userLogout(request));
    }

    /**
     * 添加用户(仅管理)
     * @param userAddDTO
     * @return 用户id
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public Result<Long> addUser(@RequestBody UserAddDTO userAddDTO) {
        ThrowUtils.throwIf(userAddDTO == null,ErrorCode.PARAMS_ERROR);
        User userEntity = UserAssembler.toUserEntity(userAddDTO);
        return Result.success(userApplicationService.saveUser(userEntity));
    }
    /**
     * 根据id获取用户(仅管理)
     * @param userId
     * @return 用户信息
     */
    @PostMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public Result<User> getUserById(long userId) {
        ThrowUtils.throwIf(userId <= 0, ErrorCode.PARAMS_ERROR);
        User user = userApplicationService.getUserById(userId);
        ThrowUtils.throwIf(user == null, ErrorCode.NOT_FOUND_ERROR);
        return Result.success(user);
    }
    /**
     * 根据id获取用户VO
     * @param userId
     * @return 脱敏后的用户信息
     */
    @PostMapping("/get/vo")
    public Result<UserVO> getUserVOById(long userId) {
        User user = getUserById(userId).getData();
        return Result.success(userApplicationService.getUserVO(user));
    }

    /**
     * 根据id删除用户(仅管理)
     * @param deleteRequest
     * @return 是否删除成功
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public Result<Boolean> deleteUserById(@RequestBody DeleteRequest deleteRequest) {
        ThrowUtils.throwIf(deleteRequest == null || deleteRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        return Result.success(userApplicationService.deleteUser(deleteRequest));
    }

    /**
     * 更新用户(仅管理)
     * @param userUpdateDTO
     * @return 是否更新成功
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public Result<Boolean> updateUser(@RequestBody UserUpdateDTO userUpdateDTO) {
        ThrowUtils.throwIf(userUpdateDTO == null || userUpdateDTO.getId() == null, ErrorCode.PARAMS_ERROR);
        User userEntity = UserAssembler.toUserEntity(userUpdateDTO);
        userApplicationService.updateUser(userEntity);
        return Result.success(true);
    }

    /**
     * 分页获取用户列表(仅管理)
     * @param userQueryDTO
     * @return 分页用户列表
     */
    @PostMapping("/list/page/vo")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public Result<Page<UserVO>> listUserByPage(@RequestBody UserQueryDTO userQueryDTO) {
        ThrowUtils.throwIf(userQueryDTO == null,ErrorCode.PARAMS_ERROR);
        return Result.success(userApplicationService.listUserVOByPage(userQueryDTO));
    }
}
